//
//  HMStatusOriginalView.swift
//  01-SinaWeibo
//
//  Created by 周英培 on 16/5/24.
//  Copyright © 2016年 周英培. All rights reserved.
//  微博首页自定义视图 原创微博视图

import UIKit

import SnapKit

import SDWebImage
/*
 View里面进行子控件的布局
 */

class HMStatusOriginalView: UIView {
    
    //设置约束对象属性
    var bottomCons : Constraint?
    
    
    //设置模型数据
    var statusViewModel: HMStatusViewModel? {
    
        didSet {
        
            //用户头像
//            let url = NSURL(string: statusViewModel?.status?.user?.profile_image_url ?? "")
            headIcon.sd_setImageWithURL(statusViewModel?.headImageURL)
            
            nameLabel.text = statusViewModel?.status?.user?.name
            
//            timeLabel.text = statusViewModel?.status?.created_at
            
            timeLabel.text = statusViewModel?.time
            
//            sourceLabel.text = statusViewModel?.status?.source
            sourceLabel.text = statusViewModel?.sourceText
            
            contentLabel.text = statusViewModel?.status?.text
            
            verifiedIcon.image = statusViewModel?.verifiedImage
            
            mbrankIcon.image = statusViewModel?.mbrankImage
            
            //原创微博配图
//            orginalPicker.pictures = statusViewModel?.status?.pic_urls
            //根据有无原创配图来设置底部约束 让有无配图的情况下底部约束都一样
            //先卸载约束
            bottomCons?.uninstall()
            //这个地方 即使没有 但是是空数组 所以还是有值的 所以要双重判断 判断数组里面有值时，才进入这个分支
            if let urls = statusViewModel?.status?.pic_urls  where urls.count > 0{
                //有配图
                //设置配图数据
                orginalPicker.pictures = urls
                
                //让配图显示
                orginalPicker.hidden = false
                
                //更新约束
//                self.snp_makeConstraints(closure: { (make) in
//                    self.bottomCons = make.bottom.equalTo(orginalPicker.snp_bottom).offset(margin).constraint
//                })
                self.snp_updateConstraints(closure: { (make) in
                    self.bottomCons = make.bottom.equalTo(orginalPicker.snp_bottom).offset(margin).constraint
                })
                
            } else {
            
                //没有配图
                //隐藏
                orginalPicker.hidden = true
                
                //更新约束
//                self.snp_makeConstraints(closure: { (make) in
//                    self.bottomCons = make.bottom.equalTo(contentLabel.snp_bottom).offset(margin).constraint
//                })
                self.snp_updateConstraints(closure: { (make) in
                    self.bottomCons = make.bottom.equalTo(contentLabel.snp_bottom).offset(margin).constraint
                })
            
            }
            
            
            
//            //认证图片
//            if statusViewModel?.status?.user?.verified == 1 {
//                
//            //是认证用户 显示认证图片
//            verifiedIcon.hidden = false
//                
//            } else {
//            
//            verifiedIcon.hidden = true
//            
//            }
//            
//            //用户等级
//            if statusViewModel?.status?.user?.mbrank > 0 && statusViewModel?.status?.user?.mbrank < 7 {
//                
//                mbrankIcon.image = UIImage(named: "common_icon_membership_level\(statusViewModel?.status?.user?.mbrank ?? 0)")
//            }
        
        }
    
    }
    

    
    //在里面设置UI
    override init(frame: CGRect) {
        super.init(frame: frame)
        
//        backgroundColor = UIColor.orangeColor()
        
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK:懒加载
    //分割条
    private lazy var sprLineView : UIView = UIView()
    
    //用户头像
    private lazy var headIcon : UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    //认证
    private lazy var verifiedIcon : UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    //用户名
    private lazy var nameLabel : UILabel = UILabel(title: "周杰伦", font: 14, textColor: UIColor.blackColor())
    
    //用户等级 是否是VIP
    private lazy var mbrankIcon : UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    //时间
    private lazy var timeLabel : UILabel = UILabel(title: "11:11", font: 12, textColor: UIColor.darkGrayColor())
    
    //微博来源
    private lazy var sourceLabel : UILabel = UILabel(title: "周杰伦", font: 12, textColor: UIColor.darkGrayColor())
    
    //微博内容
    private lazy var contentLabel : UILabel = UILabel(title: "情字何解，怎落笔都不对，而我独缺，你一生的 了解，我等春雷，来提醒你爱谁;我想大声宣布，对你依依不舍，我想就这样牵着你的手说不放开", font: 12, textColor: UIColor.darkGrayColor(),alignment:.Left)
    
    //原创视图里面的配图
    private lazy var orginalPicker : HMStatusPickerView = HMStatusPickerView()
    
    
    //MARK:设置UI 设置约束
    private func setUI() {
        addSubview(sprLineView)
        //设置分割条的颜色
        sprLineView.backgroundColor = UIColor.blueColor()
        addSubview(headIcon)
        addSubview(verifiedIcon)
        addSubview(nameLabel)
        addSubview(mbrankIcon)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(orginalPicker)
        
        //裁剪头像
//        headIcon.cornerRadius = 25
        headIcon.layer.cornerRadius = 25
//        headIcon.layer.masksToBounds = true
        headIcon.clipsToBounds = true
        
        //给分隔条设置约束
        sprLineView.snp_makeConstraints { (make) in
            make.top.left.right.equalTo(self)
            make.height.equalTo(margin)
        }
        
        //给用户头像设置约束
        headIcon.snp_makeConstraints { (make) in
            make.top.equalTo(sprLineView.snp_bottom).offset(margin)
            make.left.equalTo(self).offset(margin)
            make.height.width.equalTo(50)
        }
        
        //给认证设置约束
        verifiedIcon.snp_makeConstraints { (make) in
            make.right.bottom.equalTo(headIcon)
        }
    
        //给用户名设置约束
        nameLabel.snp_makeConstraints { (make) in
            make.left.equalTo(headIcon.snp_right).offset(margin)
            make.top.equalTo(headIcon.snp_top)
        }
        
        //给用户等级 是否是VIP 设置约束
        mbrankIcon.snp_makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp_right).offset(margin)
            make.top.equalTo(nameLabel.snp_top)
        }
        
        //给时间设置约束
        timeLabel.snp_makeConstraints { (make) in
            make.left.equalTo(headIcon.snp_right).offset(margin)
            make.bottom.equalTo(headIcon.snp_bottom)
        }
        
        //给微博来源设置约束
        sourceLabel.snp_makeConstraints { (make) in
            make.left.equalTo(timeLabel.snp_right).offset(margin)
            make.bottom.equalTo(timeLabel.snp_bottom)
        }
        
        //给微博内容设置约束
        contentLabel.snp_makeConstraints { (make) in
            make.left.equalTo(self).offset(margin)
            make.top.equalTo(headIcon.snp_bottom).offset(margin)
        }
        
        //给原创微博配图设置约束
        orginalPicker.snp_makeConstraints { (make) in
            make.left.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(margin)
            
            //设置测试配图尺寸
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
    
        //给微博内容底部和view之间设置约束 防止微博内容底部超出
        self.snp_makeConstraints { (make) in
            
            //记录原创微博的底部约束
          self.bottomCons = make.bottom.equalTo(orginalPicker.snp_bottom).offset(margin).constraint
        }
    }

}
